/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2013
 */

package com.smg.community.service;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

import javacommon.base.*;

import cn.org.rapid_framework.page.*;

import com.smg.community.model.*;
import com.smg.community.dao.*;
import com.smg.community.setting.Cache;
import com.smg.community.setting.Constant;
import com.smg.community.setting.ProjectSetting;
import com.smg.community.vo.query.*;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional
public class SmgTopicManager extends BaseManager<SmgTopic,java.lang.Integer>{

	private SmgTopicDao smgTopicDao;
	/**增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,请注意大小写*/
	public void setSmgTopicDao(SmgTopicDao dao) {
		this.smgTopicDao = dao;
	}
	public EntityDao getEntityDao() {
		return this.smgTopicDao;
	}
	
	@Transactional(readOnly=true)
	public Page findPage(SmgTopicQuery query) {
		return smgTopicDao.findPage(query);
	}
	public List<SmgTopic> getByQuery(SmgTopicQuery query) {
		return smgTopicDao.getByQuery(query);
	}
	public List<SmgTopic> getFourHotTopicList(int cityNo) {
		if ( Cache.INDEX_FOUR_TOPIC_LIST_BY_REPLYCOUNT == null){
			Cache.INDEX_FOUR_TOPIC_LIST_BY_REPLYCOUNT =  smgTopicDao.getFourHotTopicList(cityNo);
		}
		if ( ProjectSetting.USE_CACHE){
			return Cache.INDEX_FOUR_TOPIC_LIST_BY_REPLYCOUNT;
		}else{
			return  smgTopicDao.getFourHotTopicList(cityNo);
		}
	}
	public void updateLastReply(SmgTopicReply reply) {
		smgTopicDao.updateLastReply(reply);
	}
	public void updateClickCount(int topicId) {
		smgTopicDao.updateClickCount(topicId);
	}
	public int getPageNumberAfterReply(int topicId) {
		int replyCount = smgTopicDao.getReplyCountById(topicId);
		if ( replyCount < 10){
			return 1;
		}
		if  ( replyCount % 10 == 0){
			return replyCount/10 ;
		}else
			return replyCount / 10  + 1;
	}
	public Page<SmgTopic> findActivityPage(SmgTopicQuery query) {
		return  smgTopicDao.findActivityPage(query);
	}
	public List<SmgTopic> getThreeActivityListOnline(int cityNo) {
		List<SmgTopic> temp = null;
	
		if ( Cache.threeActivityList == null ){
			temp= smgTopicDao.getThreeActivityList(cityNo);
			if ( temp.size() < 3){
				SmgTopicQuery query = new SmgTopicQuery();
				query.setCityNo(cityNo);
				query.setTopicType(Constant.TOPIC_TYPE_ACTIVITY);
				query.setOrderBy("createdate desc");
				query.setStart(0);
				query.setLimit(3-temp.size());
				temp.addAll(this.getByQuery(query));
			}
			Cache.threeActivityList  = temp;
		}
		if  ( ProjectSetting.USE_CACHE ){
			return Cache.threeActivityList;
		}else{
			temp= smgTopicDao.getThreeActivityList(cityNo);
			if ( temp.size() < 3){
				SmgTopicQuery query = new SmgTopicQuery();
				query.setCityNo(cityNo);
				query.setTopicType(Constant.TOPIC_TYPE_ACTIVITY);
				query.setOrderBy("createdate desc");
				query.setStart(0);
				query.setLimit(3-temp.size());
				temp.addAll(this.getByQuery(query));
			}
			
		}
		return temp;
	}
	public List<SmgTopic> geteActivityListOnline(int cityNo,int size) {
		SmgTopicQuery query = new SmgTopicQuery();
		query.setCityNo(cityNo);
		query.setTopicType(Constant.TOPIC_TYPE_ACTIVITY);
		query.setOrderBy("createdate desc");
		query.setStart(0);
		query.setLimit(size);
		return this.getByQuery(query);
	}
	public Page<SmgTopic> findMemberJioinActivityPage(SmgTopicQuery query) {
		return smgTopicDao.findMemberJioinActivityPage(query);
		
	}
	public Page<SmgTopic> findMemberReplyTopicPage(SmgTopicQuery query) {
		return smgTopicDao.findMemberReplyTopicPage(query);
	}
	public void updateTopType(SmgTopic topic) {
		smgTopicDao.updateTopType(topic);
	}
	public void updateTopicTitleAndCont(SmgTopic topic) {
		smgTopicDao.updateTopicTitleAndCont(topic);
	}
	
}
